<link rel="stylesheet" href="css/chosen.css" />
<script src="js/chosen.jquery.js" type="text/javascript"></script>
        <script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$("#tgl_jual").datepicker({
					dateFormat  : "yy-mm-dd",        
					  changeMonth : true,
					  changeYear  : true					
				});
				
				$("#id_customer").change(function(){
					$.ajax({						
						url:'getAlamat.php',
						dataType: "html",
						type:'POST',
						data:'id_customer='+$(this).val(),
						success: function(data) {
							var obj = $.parseJSON(data);						
							if(data=="[]"){
								$("#alamat").val('');
										
							}else{							
								$("#alamat").val(obj[0].alamat);								
							}
						}
					})
				})
				/*$("#jumlah_rts").change(function(){
					$.ajax({						
						url:'cekRts.php',
						dataType: "html",
						type:'POST',
						data:'jumlah_rts='+$(this).val(),
						success: function(data) {
							//var obj = $.parseJSON(data);						
							if(data=='OK')                  
							  {
								  $('#imgTry').html('OK, Nilai Mencukupi!!!');
							  }
							  else
							  {
								  $('#imgTry').html(' Nilai Tidak Mencukupi!!!');
							  }
						}
					})
				})
				
				$('#jumlah_rts').keyup(function(){
					var jumlah = $('#jumlah').val();
					var jumlah_reject = $('#jumlah_reject').val();	
					var jumlah_rts = $(this).val();	

					var total = parseInt(jumlah_rts)+parseInt(jumlah_reject);
					//alert(total)
					if(parseInt(total)!=parseInt(jumlah)){
						alert('salah');	
					}
				});
				
				$('#jumlah_reject').keyup(function(){
					var jumlah = $('#jumlah').val();
					var jumlah_reject = $(this).val();	
					var jumlah_rts = $('#jumlah_rts').val();	
					
					var total = parseInt(jumlah_reject)+parseInt(jumlah_rts);
					//alert(total)
					if(parseInt(total)!=parseInt(jumlah)){
						alert('salah');	
					}
				})
				
				$('#jumlah').keyup(function(){
					var jumlah = $(this).val();
					var jumlah_reject = $('#jumlah_reject').val();	
					var jumlah_rts = $('#jumlah_rts').val();	
					//alert(parseInt(jumlah_reject))
					var total = parseInt(jumlah_rts)+parseInt(jumlah_reject);
					if ((jumlah_reject!='') && (jumlah_rts!='')){
						if(parseInt(total)!=parseInt(jumlah)){
							alert('salah');	
						}
					}
				})*/
				
				
			});
		</script>
<?php
if($_SESSION['bkeluar']!=1){
	echo "Akses tidak diizinkan";
	exit();
}

$act = filter($_GET['act']);
$field = filter($_GET['field']);
$q = filter($_GET['q']);

if(empty($act)){	
?>		
					<!-- box / title -->
					<div class="title">
						<h5>Data Barang keluar</h5>								
						<div class="search global-nav">
						  <form method="GET" class="form-search js-search-form" action="index.php?pages=brg_kluar" id="global-nav-search" style="width:350px">
								<input type="hidden" name="pages" value="brg_kluar">
								<select name="field" id="birthday_month" aria-label="Month" class="" >
									<option value="" <?php echo ($field=="")?"selected":"";?>>Cari Berdasarkan:</option>
									<option value="tgl_jual" <?php echo ($field=="tgl_jual")?"selected":"";?>>Tgl Jual</option>
									<option value="nota" <?php echo ($field=="nota")?"selected":"";?>>Nota</option>
								</select>

				            	<label class="visuallyhidden" for="search-query">Search query</label>
				            	<input class="search-input" type="text" id="search-query" placeholder="Search" name="q" value="<?php echo $q;?>">
				            	<span class="search-icon js-search-action">
				              		<button type="submit" class="icon nav-search" tabindex="-1">
				              			<span class="visuallyhidden">Search</span>
				              		</button>
				            	</span>
				            	<input disabled="disabled" class="search-input search-hinting-input" type="text" id="search-query-hint" autocomplete="off" spellcheck="false">				              					              
							</form>
						</div>
					</div>
					<!-- end box / title -->
					<div class="table">										
						<table>
							<thead>
								<tr>
									<th class="selected"><input type="checkbox" class="checkall" /></th>
									<th class="action">No.</th>
									<th class="left">Customer</th>
									<th>Tgl Jual</th>
                                	<!--<th>Nota</th>-->
									<th>Sales</th>
									<th>Jumlah</th>
                                    <th>Jumlah Cad</th>
                                    <th>Jumlah RTS</th>
									<th>Jumlah Perbaikan</th>
                              <th class="left">Satuan</th>
									<th class="action" colspan="2"><a href="?pages=brg_kluar&act=edit" class="icon-plus-sign" title="Tambah Data Baru"></a></th>									
								</tr>
							</thead>
							<tbody>
							<?php			

							$q2=$q;
							if($field=='status'){
								$q2 = (strtolower($q)=='aktif')?'1':'0';
							}
							
							$addQuery='';


							if(!empty($field))
								$addQuery .= " WHERE $field LIKE '%$q2%' ";

							$batas=10;
							$pg=(int) $_GET['pg'];
							if(empty($pg))
							{
								$posisi=0;
								$pg=1;
							}
							else
							{
								$posisi = ($pg-1) * $batas;
							}
							$sqlSup1 = mysql_query("SELECT * FROM tb_penjualan $addQuery ORDER BY id_penjualan DESC LIMIT $posisi, $batas");
							
							$no=$posisi+1;
							while($dataSup = mysql_fetch_array($sqlSup1)){								

								$status = ($dataSup['status']==1)? "Aktif":"Non Aktif";
							?>
								<tr>
									<td class="selected"><input type="checkbox" /></td>
									<td class="center"><?php echo $no;?></td>
                                     <?php 
										  $sql=mysql_query("select * from tb_customer where id_customer='$dataSup[id_customer]'");
										  $sql2=mysql_fetch_array($sql);
									?>
									<td><?php echo $sql2['nama'];?></td>
									<td><?php echo $dataSup['tgl_jual'];?></td>
									<!--<td><?php echo $dataSup['nota'];?></td>-->
                                    <?php 
										  $sql=mysql_query("select * from tb_sales where id_sales='$dataSup[id_sales]'");
										  $sql2=mysql_fetch_array($sql);
									?>
                                    <td><?php echo $sql2['nama'];?></td>
                                    <td><?php echo $dataSup['jumlah'];?></td>
                                    <td><?php echo $dataSup['jumlah_cad'];?></td>
                                    <td><?php echo $dataSup['jumlah_rts'];?></td>
                                    <td><?php echo $dataSup['jumlah_reject'];?></td>
									<td><?php echo $dataSup['satuan'];?></td>	
									<td class="action"><a href="?pages=brg_kluar&act=edit&id=<?php echo $dataSup['id_penjualan'];?>" class="icon-edit" title="Ubah Data"></a></td>
									<td class="action">
										<a href="#" title="Hapus Data" onClick="if(confirm('Anda akan menghapus data ini?')) window.location='?pages=brg_kluar&act=del&id=<?php echo $dataSup['id_penjualan'];?>';" class="icon-trash"></a>
									</td>								
								</tr>
							<?php	
								$no++;				
							}

							$sqlSup2 = mysql_query("SELECT * FROM tb_penjualan $addQuery ORDER BY id_penjualan DESC");
							$jumlahData = mysql_num_rows($sqlSup2);

							$file="?pages=brg_kluar&field=$field&q=$q";
							$jmlhalaman=ceil($jumlahData/$batas);
	
							?>															
							</tbody>
						</table>
						<!-- pagination -->
						<div class="pagination pagination-left">
							<div class="results">
								<span><?php echo "Showing Results ".($posisi+1)."-".($no-1)." of $jumlahData";?></span>
							</div>
							<ul class="pager">
								<?php
								//link ke halaman sebelumnya (previous)
								if($pg > 1){
									$previous=$pg-1;
									echo "<li><a href=$file&pg=1>&laquo; First</a></li>
									<li><a href=$file&pg=$previous>&laquo; Prev</a></li> ";
								}else{ 
									echo "<li class='disabled'>&laquo; First</li>
									<li class='disabled'>&laquo; Prev</li> ";
								}
								?>

								<?php
								//Link untuk Angka
								$angka=($pg > 3 ? " <li class='separator'>...</li> " : " ");
								for($i=$pg-2;$i<$pg;$i++)
								{
								  if ($i < 1) 
								      continue;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= " <li class='current'>$pg</li>";
								for($i=$pg+1;$i<($pg+3);$i++)
								{
								  if ($i > $jmlhalaman) 
								      break;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= ($pg+2<$jmlhalaman ? " <li class='separator'>...</li> <li><a href='$file&pg=$jmlhalaman'>$jmlhalaman</a></li>" : " ");

								echo "$angka";
								?>
								

								<?php
								//link kehalaman berikutnya (Next)
								if($pg < $jmlhalaman)
								{
									$next=$pg+1;
									echo "<li><a href='$file&pg=$next'>Next &raquo;</a></li>
									<li><a href='$file&pg=$jmlhalaman'>Last &raquo;</a></li>";
								}
								else
								{ 
									echo "<li class='disabled'>Next &raquo;</li>
									<li class='disabled'>Last &raquo;</li> ";
								}		
								?>													
							</ul>
						</div>

						<!-- end pagination -->
						<!-- table action -->
						
						<!-- end table action -->						
					<?php
						$sqlRts = mysql_query("SELECT * FROM tb_temp_rts LIMIT 1");
						$getRts = mysql_fetch_array($sqlRts);
						?>
						<p>&nbsp;</p>
						<p style="font:bold 14px Tahoma"><i>Total Stock Ready to Sale : <?php echo $getRts['stock_rts'];?> Karton</i></p>
                        <p style="font:bold 14px Tahoma"><i>Total Stock Ready to Perbaikan : <?php echo $getRts['stock_perbaikan'];?> Karton</i></p>					
					</div>

<?php
}elseif ($act=='edit') {

	$id_penjualan = (int) filter($_GET['id']);

	$sqlSup = mysql_query("SELECT * FROM tb_penjualan WHERE id_penjualan=$id_penjualan");
	$dataSup = mysql_fetch_array($sqlSup);

	if(!empty($id_penjualan)){
		?>
		<!-- box / title -->
		<div class="title">
			<h5>Ubah Data Barang keluar</h5>														
		</div>
		<!-- end box / title -->
		<?php
	}else{
		?>
		<!-- box / title -->
		<div class="title">
			<h5>Tambah Data Barang keluar</h5>														
		</div>
		<!-- end box / title -->
		<?php
	}

?>	

	<div class="table">
		<div style="float:left">
		<form method="POST" action="?pages=brg_kluar&act=update">
		<input type="hidden" name="id_penjualan" value="<?php echo $dataSup['id_penjualan'];?>">
		<table style="width:auto">
		   	<tbody>
		   		<tr>
					<td><label>Customer</label></td>
					<td>:</td>
					<td><select class="select" style="width: 210px;" name="id_customer" id="id_customer" data-placeholder=" Pilih Nama Customer" required="required">
					  <option ></option>
					  <?php
                            $select_jur = mysql_query("SELECT * FROM tb_customer");
                            while($select_jur2 = mysql_fetch_array($select_jur)){
							 if($dataSup['id_customer']==$select_jur2['id_customer']){
						  	$selected = " selected ";
						  }else{
						  	$selected = "";
						  }
                      ?>
					  <option <?php echo $selected;?> value="<?php echo"$select_jur2[id_customer]";?>"><?php echo"$select_jur2[nama]";?></option>
					  <?php
                            }
                      ?>
					  </select></td>
					<td>                    
					<td><label>Jumlah</label></td>
					<td>:</td>
					<td><input type="text" value="<?php echo $dataSup['jumlah'];?>" size="30" name="jumlah" id="jumlah" placeholder="Jumlah Penjualan" required="required"/>
                    <input type="hidden" value="<?php echo $dataSup['jumlah'];?>" size="30" name="jumlahx" id="jumlah" placeholder="Jumlah Penjualan" required="required"/></td>
		   		</tr>	
                <tr>
					<td height="24"><label>Alamat</label></td>
			    <td>:</td>
					<td><input type="text" value="<?php echo $dataSup['alamat'];?>" size="30" name="alamat" id="alamat" placeholder="Alamat Customer" readonly="readonly" /></td>
					<td>&nbsp;</td>
					<td><label>Jumlah cad</label></td>
					<td>:</td>
					<td><input type="text" value="<?php echo $dataSup['jumlah_cad'];?>" size="30" name="jumlah_cad" id="jumlah_cad" placeholder="Jumlah Cadangan" required="required"/>
                    <input type="hidden" value="<?php echo $dataSup['jumlah_cad'];?>" size="30" name="jumlah_cadx" id="jumlah_cad" placeholder="Jumlah Cadangan" required="required"/></td>
				</tr>
                <tr>
                  <td><label>Tgl Jual</label></td>
					<td>:</td>
					<td><input type="text"  value="<?php echo $dataSup['tgl_jual'];?>" size="30" name="tgl_jual" id="tgl_jual" placeholder="Tgl. Penjualan" required="required"/></td>
					<td>&nbsp;</td>
					<td><label>Jumlah RTS</label></td>
					<td>:</td>
					<td><input type="text" value="<?php echo $dataSup['jumlah_rts'];?>" size="30" name="jumlah_rts" id="jumlah_rts" placeholder="jumlah Barang dari RTS" required="required"/>
                    <input type="hidden" value="<?php echo $dataSup['jumlah_rts'];?>" size="30" name="jumlah_rtsx" id="jumlah_rts" placeholder="jumlah Barang dari RTS" required="required"/>
					  <span id="imgTry">&nbsp;</span></td>
				</tr>		   	
			   	
				<tr>
				  <td><label>Sales</label></td>
				  <td>:</td>
				  <td><select class="select" style="width: 210px;" name="id_sales" id="id_sales" data-placeholder=" Pilih Nama Sales" required="required">
				    <option ></option>
				    <?php
                            $select_jur = mysql_query("SELECT * FROM tb_sales");
                            while($select_jur2 = mysql_fetch_array($select_jur)){
							 if($dataSup['id_sales']==$select_jur2['id_sales']){
						  	$selected = " selected ";
						  }else{
						  	$selected = "";
						  }
                      ?>
				    <option <?php echo $selected;?> value="<?php echo"$select_jur2[id_sales]";?>"><?php echo"$select_jur2[nama]";?></option>
				    <?php
                            }
                      ?>
				    </select></td>
					<td>&nbsp;</td>
					<td><label>Jumlah Perbaikan</label></td>
					<td>:</td>
					<td><input type="text" value="<?php echo $dataSup['jumlah_reject'];?>" size="30" name="jumlah_reject" id="jumlah reject" placeholder="Jumlah Barang dari Perbaikan" required="required"/>
                    <input type="hidden" value="<?php echo $dataSup['jumlah_reject'];?>" size="30" name="jumlah_rejectx" id="jumlah reject" placeholder="Jumlah Barang dari Perbaikan" required="required"/></td>
				</tr>
                <tr>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>                    
					<td><label>Satuan</label></td>
					<td>:</td>
					<td><input type="text" value="Karton" size="30" name="satuan" id="satuan"  readonly="true" required="required"/></td>
                </tr>
                <tr>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
				 	<td>&nbsp;</td> 
			   	</tr>
                <tr>
					<td colspan="7"  align="center"><input type="hidden" value="" size="15" name="id">
					  <input type="submit" value="Simpan" name="submit" class="btn btn-primary">
				    <input type="Reset" value="Reset" class="btn"></td>
				</tr>
		 	</tbody>
		</table>
		<div style="float:left;margin-left:20px">
		</form>
	</div>
<?php
$sqlRts = mysql_query("SELECT * FROM tb_temp_rts LIMIT 1");
$getRts = mysql_fetch_array($sqlRts);

?>
<p>&nbsp;</p>
<p style="font:bold 14px Tahoma"><i>Total Stock Ready to Sale : <?php echo $getRts['stock_rts'];?> Karton</i></p>					
<p style="font:bold 14px Tahoma"><i>Total Stock Ready to Perbaikan : <?php echo $getRts['stock_perbaikan'];?> Karton</i></p>					
</div>
<?php
}elseif ($act=='update') {
	# code...
	$id_penjualan = (int) $_POST['id_penjualan'];
	$id_customer = $_POST['id_customer'];
	$tgl_jual = $_POST['tgl_jual'];
	$nota = $_POST['nota'];
	$id_sales=$_POST['id_sales'];
	
	$jumlah = $_POST['jumlah'];
	$jumlah_cad = $_POST['jumlah_cad'];
	$jumlah_rts = $_POST['jumlah_rts'];
	$jumlah_reject = $_POST['jumlah_reject'];
	
	$jumlahx = $_POST['jumlahx'];
	$jumlah_cadx = $_POST['jumlah_cadx'];
	$jumlah_rtsx = $_POST['jumlah_rtsx'];
	$jumlah_rejectx = $_POST['jumlah_rejectx'];
	
	$satuan = $_POST['satuan'];
	
	$total1= $jumlah + $jumlah_cad;
	$total2= $jumlah_rts + $jumlah_reject;
	
	$sql_rts=mysql_query("SELECT stock_rts from tb_temp_rts");
	$sql_rts2=mysql_fetch_array($sql_rts);
	$b=$sql_rts2["stock_rts"];

	$sql_reject=mysql_query("SELECT stock_perbaikan from tb_temp_rts");
	$sql_reject2=mysql_fetch_array($sql_reject);
	$b2=$sql_reject2["stock_perbaikan"];

	$update_rts= $b - $jumlah_rts;
	$update_rts2= $b2 - $jumlah_reject;


	$update_temp_rts=mysql_query("SELECT * from tb_temp_rts");
	$update_temp_rts2=mysql_fetch_array($update_temp_rts);
	$temp2=$update_temp_rts2["stock_rts"];
	$temp3=$update_temp_rts2["stock_perbaikan"];


	$tb_penjualan=mysql_query("SELECT * from tb_penjualan where id_penjualan='$id_penjualan'");
	$tb_penjualan2=mysql_fetch_array($tb_penjualan);
	//$stok_awal=$tb_penjualan2["jumlah_rts"];
	//$stok_awal2=$tb_penjualan2["jumlah_reject"];


	$total_temp=($temp2 + $jumlah_rtsx)- $jumlah_rts;
	$total_temp2=($temp3 + $jumlah_rejectx)- $jumlah_reject;

if(!empty($id_penjualan)){
	if($total1 < $total2){
				sukses('Data RTS & Reject Anda Melebihi Jumlah Barang yang di jual', "?pages=brg_kluar&act=edit&id=$id_penjualan");
			}
		else if($total1 > $total2){
				sukses('Data RTS & Reject Anda Kurang dari Jumlah Barang yang di jual', "?pages=brg_kluar&act=edit&id=$id_penjualan");
			}
		else if($jumlah_rts > $b){
				sukses('Data RTS melebihi stok di Database', "?pages=brg_kluar&act=edit&id=$id_penjualan");
			}
		else if($jumlah_reject > $b2){
				sukses('Data Perbaikan melebihi stok di Database', "?pages=brg_kluar&act=edit&id=$id_penjualan");
			}
		else {

			$update = mysql_query("UPDATE tb_penjualan SET id_customer='$id_customer', tgl_jual='$tgl_jual', nota='$nota', id_sales='$id_sales', jumlah_cad='$jumlah_cad', jumlah='$jumlah',  jumlah_rts='$jumlah_rts', jumlah_reject='$jumlah_reject', satuan='$satuan' WHERE id_penjualan=$id_penjualan");
			$update_temp=mysql_query("UPDATE tb_temp_rts SET stock_rts='$total_temp', stock_perbaikan='$total_temp2' WHERE id_temp='1'");


						if($update && $update_temp)
							sukses('Data berhasil disimpan', '?pages=brg_kluar');
						}
}else{
		if($total1 < $total2){
				sukses('Data RTS & Reject Anda Melebihi Jumlah Barang yang di jual', '?pages=brg_kluar&act=edit');
			}
		else if($total1 > $total2){
				sukses('Data RTS & Reject Anda Kurang dari Jumlah Barang yang di jual', '?pages=brg_kluar&act=edit');
			}
		else if($jumlah_rts > $b){
				sukses('Data RTS melebihi stok di Database', '?pages=brg_kluar&act=edit');
			}
		else if($jumlah_reject > $b2){
				sukses('Data Perbaikan melebihi stok di Database', '?pages=brg_kluar&act=edit');
			}
		else {
			
			$insert = mysql_query("INSERT INTO tb_penjualan VALUES(NULL, '$id_customer', '$tgl_jual', 'NULL', '$id_sales', $jumlah_cad, '$jumlah',  '$jumlah_rts', '$jumlah_reject', '$satuan')");
			$update2 = mysql_query("UPDATE tb_temp_rts set stock_rts='$update_rts', stock_perbaikan=$update_rts2 WHERE id_temp='1'");

		  $sql_rtu=mysql_query("SELECT id_penjualan from tb_penjualan order by id_penjualan desc");
		  $sql_rtu3=mysql_fetch_array($sql_rtu);
		  $id_rtu=$sql_rtu3["id_penjualan"];
		  $nota_id="PS".$id_rtu;
		  $update_nota = mysql_query("UPDATE tb_penjualan set nota='$nota_id' WHERE id_penjualan=$id_rtu");
						
						if($insert && $update2)
							sukses('Data berhasil disimpan', '?pages=brg_kluar');
					}
				}
	
}elseif ($act=='del') {
	# DEL code...
	$id_penjualan = (int) filter($_GET['id']);

	$sql_cek = mysql_query("SELECT * from tb_penjualan where id_penjualan='$id_penjualan'");
	$cek = mysql_fetch_array($sql_cek);
	$jumlah_rtsx = $cek['jumlah_rts'];
	$jumlah_rtsy = $cek['jumlah_reject'];

	if ($cek['id_penjualan'] == true) {
		$sql_temp = mysql_query("SELECT * FROM tb_temp_rts WHERE id_temp = 1 ");
		$j_stok = mysql_fetch_array($sql_temp);
		$jml_stok = $j_stok['stock_rts'] + $jumlah_rtsx;
		$jml_stok2 = $j_stok['stock_perbaikan'] + $jumlah_rtsy;
		$sql_update_stok = mysql_query("UPDATE tb_temp_rts SET stock_rts = $jml_stok, stock_perbaikan=$jml_stok2 WHERE id_temp='1'");

	}

	/*if ($sql_update_stok) {
		sukses('Data berhasil diupdate', '?pages=brg_kluar');
	
	}*/


	$delete = mysql_query("DELETE FROM tb_penjualan WHERE id_penjualan=$id_penjualan");

	if($delete)
		sukses('Data berhasil dihapus', '?pages=brg_kluar');
}

?>		

<!--<td><label>Nota</label></td>
					<td>:</td>
					<td><input type="text" value="<?php echo $dataSup['nota'];?>" size="30" name="nota" id="nota" placeholder="Nomor Nota" required="required"/></td>-->	
					
<script type="text/javascript">
$(document).ready(function () {
	$("#id_customer, #id_sales").chosen();  
})
</script>		